vfs: integrate with CJS and ESM module loaders#63653
Open
mcollina wants to merge 1 commit into
Open
Conversation
Collaborator
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #63653 +/- ##
==========================================
- Coverage 91.96% 90.32% -1.64%
==========================================
Files 379 732 +353
Lines 166638 237271 +70633
Branches 25497 44727 +19230
==========================================
+ Hits 153242 214325 +61083
- Misses 13099 14656 +1557
- Partials 297 8290 +7993
🚀 New features to boost your workflow:
|
Member
Author
|
@joyeecheung take a look, should be easier to review. |
ShogunPanda
approved these changes
May 30, 2026
jsumners-nr
reviewed
Jun 1, 2026
| // internals may stop going through the JavaScript fs module entirely. | ||
| // Prefer module.registerHooks() or other more formal fs hooks released in the future. | ||
| source = fs.readFileSync(url); | ||
| source = loaderReadFile(url); |
There was a problem hiding this comment.
Is the comment no longer applicable?
Member
Author
There was a problem hiding this comment.
The whole point of vfs is to provide a way to avoid monkey patching. I can update the comment to point to vfs.
| const real = fs.realpathSync(path, { | ||
| [internalFS.realpathCacheKey]: realpathCache, | ||
| }); | ||
| const real = toRealPath(path); |
mcollina
added a commit
to mcollina/node
that referenced
this pull request
Jun 1, 2026
Restore the "DO NOT depend on the patchability" warnings in esm/load.js and esm/resolve.js that were dropped along with the fs imports. The warning still applies; it now also points at node:vfs as one of the formal hook mechanisms callers should reach for instead. Addresses review feedback from @jsumners-nr in nodejs#63653
Route loader fs and package.json operations through toggleable wrappers so the VFS can resolve and load modules from mounted paths. When no VFS is mounted, the wrappers take a null-check fast path with zero overhead. Hooks: - loaderStat / loaderReadFile / toRealPath / loaderLegacyMainResolve / loaderGetFormatOfExtensionlessFile in lib/internal/modules/helpers.js, consumed by cjs/loader.js, esm/resolve.js, esm/load.js and esm/get_format.js. - loaderReadPackageJSON / loaderGetNearestParentPackageJSON / loaderGetPackageScopeConfig / loaderGetPackageType, consumed by package_json_reader.js. - setLoaderFsOverrides / setLoaderPackageOverrides install / clear all hooks; clearRealpathCache exposes the helpers.js realpath cache so deregister can flush it. lib/internal/vfs/setup.js installs the overrides on first registerVFS and clears every JS-side loader cache (CJS _pathCache, CJS stat cache, realpath cache, package.json cache) on every deregister. The overrides themselves are uninstalled when the last VFS is removed so the fast path is fully restored. legacyMainResolve / extensionless-format behavior matches the C++ binding; package.json validation matches src/node_modules.cc (silently omit non-string main, throw on non-string name/type, etc). The "DO NOT depend on patchability" warnings in esm/load.js and esm/resolve.js are preserved and now point at node:vfs and module.registerHooks() as the formal hook mechanisms. Tests cover require / import / module-hooks / package.json / cache invalidation / cleanup-cycle scenarios under --experimental-vfs. Signed-off-by: Matteo Collina <hello@matteocollina.com>
6321e08 to
51b033a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Route loader fs and package.json operations through toggleable wrappers so the VFS can resolve and load modules from mounted paths.